package com.app.hmst.main;

import java.util.List;

import com.app.hmst.model.IndicatorResultVO;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


@SuppressLint("NewApi") public class GridViewImageAdapter extends BaseAdapter {

	private Activity _activity;
	private List<String> menus;
	private int imageWidth;
	private ViewHolder viewHolder;
	private IndicatorResultVO indicatorResultVO;

	public GridViewImageAdapter(Activity activity, List<String> menus,int imageWidth,IndicatorResultVO indicatorResultVO) {
		this._activity = activity;
		this.menus = menus;
		this.imageWidth = imageWidth;
		this.indicatorResultVO = indicatorResultVO;
	}

	@Override
	public int getCount() {
		return this.menus.size();
	}

	@Override
	public Object getItem(int position) {
		return this.menus.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		
		if (convertView == null) {
			viewHolder = new ViewHolder();
			LayoutInflater inflater = (LayoutInflater) _activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(imageWidth, imageWidth);
			
			convertView = inflater.inflate(R.layout.each_box_layout, null);
			viewHolder.showCase = (ImageView) convertView.findViewById(R.id.showCase);
			
			BitmapFactory.Options options = new BitmapFactory.Options();
			options.inSampleSize = 8;

			byte[] decodedString = Base64.decode(menus.get(position), Base64.DEFAULT);
			Bitmap bitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length,options);
			
			try {
				ExifInterface ei = new ExifInterface(menus.get(position));
				int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

				switch(orientation) {
				    case ExifInterface.ORIENTATION_ROTATE_90:
				    	RotateBitmap(bitmap, 90);
				        break;
				    case ExifInterface.ORIENTATION_ROTATE_180:
				    	RotateBitmap(bitmap, 180);
				        break;
				    // etc.
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
							
			viewHolder.showCase.setLayoutParams(layoutParams);
			viewHolder.showCase.setImageBitmap(bitmap);
			
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		switch (position) {
		case 0:
			indicatorResultVO.img1 = menus.get(0);
			break;
		case 1:
			indicatorResultVO.img2 = menus.get(1);
			break;
		case 2:
			indicatorResultVO.img3 = menus.get(2);
			break;
		default:
			break;
		}
		indicatorResultVO.save();
		return convertView;
	}
	public static Bitmap RotateBitmap(Bitmap source, float angle)
	{
	      Matrix matrix = new Matrix();
	      matrix.postRotate(angle);
	      return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
	}
	public class ViewHolder {
		private ImageView showCase;
		private TextView titleImage;
		private RelativeLayout relative;
	}
}
